package com.blog.backend.service.impl;

import com.blog.backend.req.UserReq;
import com.blog.backend.resp.TokenResult;
import com.blog.backend.service.UserManagerService;
import org.springframework.aop.framework.AopContext;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 用于测试applicationContext获取bean
 * @author fanggs
 * @Date 2021/7/20 0020 14:10
 */
@Service
public class AccountServiceImpl implements UserManagerService {

    @Resource
    private ApplicationContext applicationContext;


    @Override
    @Transactional
    public TokenResult login(UserReq req) {
        //事务内部方法之间调用无法生效时的做法
        //调用类内部的时候对象不再是代理对象而是this即目标对象本身
        //通过ApplicationContext.getBean()获取指定代理对象
        AccountServiceImpl bean = applicationContext.getBean(AccountServiceImpl.class);
        bean.createAccount(req);
        //第二种使用AopContext.currentProxy()获取当前代理对象
        AccountServiceImpl accountServiceImpl = (AccountServiceImpl) AopContext.currentProxy();
        accountServiceImpl.createAccount(req);
        return null;
    }

    @Override
    @Transactional
    public String createAccount(UserReq req) {
        return null;
    }

    @Override
    public Boolean layout(HttpServletRequest request) {
        return null;
    }
}
